package com.ymatou.iapi.counter.testcase;

import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.counter.parameter.setGroupBean;
import com.ymatou.iapi.counter.service.setGroupCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 更新组计数 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_setGroup {
	private static setGroupBean ssb;
	private static setGroupCall ssc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新组计数");
	}

	@Before
	public void caseUp() {
		ssb = new setGroupBean();
		ssc = new setGroupCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ssc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_setGroup_001() {
		Logger.start(true,"更新组计数成功,action1增加计数");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssb.setAction(1);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(200, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, ssc.getMsg(), "验证Msg");
			Logger.verifyEquals(10 + value, ssc.getData().get("c1"), "验证计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_setGroup_002() {
		Logger.start(true,"更新组计数成功,action2减少计数");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c2", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c2");
			ssb.setAction(2);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(200, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, ssc.getMsg(), "验证Msg");
			Logger.verifyEquals(10 - value, ssc.getData().get("c2"), "计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_setGroup_003() {
		Logger.start(true,"更新组计数成功,action3设置计数");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c3", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c3");
			ssb.setAction(3);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(200, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, ssc.getMsg(), "验证Msg");
			Logger.verifyEquals(value, ssc.getData().get("c3"), "验证计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_setGroup_004() {
		Logger.start(true,"更新组计数成功,action4计数清零");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssb.setAction(4);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(200, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, ssc.getMsg(), "验证Msg");
			Logger.verifyEquals(0, ssc.getData().get("c1"), "验证计数器的值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_setGroup_005() {
		Logger.start(false,"不输入AppId,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssb.setAction(1);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains("appId:may not be empty", ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_setGroup_006() {
		Logger.start(false,"不输入TargetId,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setCounter("c1");
			ssb.setAction(1);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains("targetId:may not be empty", ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_setGroup_007() {
		Logger.start(false,"不输入Counter,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setAction(1);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains("counter:may not be empty", ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_setGroup_008() {
		Logger.start(false,"不输入Action,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains("未能找到相应的Action", ssc.getMsg(), "验证Msg包含信息" );
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_setGroup_009() {
		Logger.start(false,"输入错误的AppId,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("errTest");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssb.setAction(1);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains("配置节点不存在", ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_setGroup_010() {
		Logger.start(false,"输入错误的Counter,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			Logger.comment("给TargetId为"+TargetId + ":添加计数器值: 10");
			new CounterCallService().setGroup("Test", "c1", TargetId, 1, 10);
			int value = new Random().nextInt(5) + 1;
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("errc1");
			ssb.setAction(1);
			ssb.setValue(value);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains("配置节点不存在", ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}